using System;
using System.IO;
using NUnit.Framework;
using TDUnit.Common.Logging;
using TDUnit.Loggers;

namespace TDUnit.Tests
{
    [TestFixture]
    public class StreamLoggerTests
    {

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void CreateLoggerWithNullStream_ShouldThrowEx()
        {
            new StreamLogger(null);
        }

        [Test]
        [ExpectedException(typeof(LoggingException))]
        public void CreateLoggerWithStreamThatCannotWrite_ShouldThrowEx()
        {
            var stream = new MemoryStream(new byte[1], false);
            new StreamLogger(stream);
        }

        [Test]
        public void WriteLogEntry()
        {
            const string theLogEntry = "This is a log entry.";
            var date = DateTime.Now;
            var messageType = MessageType.Information;
            var expectedEntry =
                string.Format("{0}: ({1}) {2}",
                messageType,
                date.ToString("yyyyMMddhhmmss"),
                theLogEntry);

            var stream = new MemoryStream();

            using (var logger = new StreamLogger(stream))
            {
                logger.LogMessage(theLogEntry, messageType);

                stream.Position = 0;
                Assert.AreEqual(expectedEntry, new StreamReader(stream).ReadLine());
            }
        }
    }
}